package com.ruoyi.project.orderWorks.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.dentalOrder.domain.DentalOrder;
import com.ruoyi.project.dentalOrder.service.IDentalOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.orderWorks.domain.OrderWorks;
import com.ruoyi.project.orderWorks.service.IOrderWorksService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 订单操作记录Controller
 * 
 * @author wyh
 * @date 2024-09-11
 */
@RestController
@RequestMapping("/orderWorks/orderWorks")
public class OrderWorksController extends BaseController
{
    @Autowired
    private IOrderWorksService orderWorksService;

    @Autowired
    private IDentalOrderService dentalOrderService;

    /**
     * 查询订单操作记录列表
     */
    @PreAuthorize("@ss.hasPermi('orderWorks:orderWorks:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderWorks orderWorks)
    {
        Long deptId = SecurityUtils.getDeptId();
        if(!deptId.equals(103L)){
            orderWorks.setDeptId(deptId);
        }

        startPage();
        List<OrderWorks> list = orderWorksService.selectOrderWorksList(orderWorks);
        return getDataTable(list);
    }

    /**
     * 导出订单操作记录列表
     */
    @PreAuthorize("@ss.hasPermi('orderWorks:orderWorks:export')")
    @Log(title = "订单操作记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderWorks orderWorks)
    {
        List<OrderWorks> list = orderWorksService.selectOrderWorksList(orderWorks);
        ExcelUtil<OrderWorks> util = new ExcelUtil<OrderWorks>(OrderWorks.class);
        util.exportExcel(response, list, "订单操作记录数据");
    }

    /**
     * 获取订单操作记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('orderWorks:orderWorks:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderWorksService.selectOrderWorksById(id));
    }

    /**
     * 新增订单操作记录
     */
    @PreAuthorize("@ss.hasPermi('orderWorks:orderWorks:add')")
    @Log(title = "订单操作记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderWorks orderWorks)
    {
        return toAjax(orderWorksService.insertOrderWorks(orderWorks));
    }

    /**
     * 修改订单操作记录
     */
    @PreAuthorize("@ss.hasPermi('orderWorks:orderWorks:edit')")
    @Log(title = "订单操作记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderWorks orderWorks)
    {
        orderWorksService.updateOrderWorks(orderWorks);

        String outTradeNo = orderWorks.getOutTradeNo();
        boolean flag = true;

        OrderWorks orderWorks1 = new OrderWorks();
        orderWorks1.setOutTradeNo(outTradeNo);
        List<OrderWorks> orderWorks2 = orderWorksService.selectOrderWorksList(orderWorks1);
        for(OrderWorks o:orderWorks2){
            String status = o.getStatus();
            if(status.equals("待处理")){
                flag = false;
            }
        }

        DentalOrder dentalOrder = dentalOrderService.selectDentalOrderByOutTradeNo(outTradeNo);
        if(flag){
            dentalOrder.setStatus("已完成");
            dentalOrderService.updateDentalOrder(dentalOrder);
        }

        return success("操作成功");
    }

    /**
     * 删除订单操作记录
     */
    @PreAuthorize("@ss.hasPermi('orderWorks:orderWorks:remove')")
    @Log(title = "订单操作记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderWorksService.deleteOrderWorksByIds(ids));
    }
}
